package cate.game.demo.rpc;

import cate.game.GameBody;
import cp.solution.rpc.path.RpcDebugPath;
import cp.solution.util.demo.vo.DemoAppleVo;
import cp.solution.util.demo.vo.DemoBananaVo;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DemoRpcLocal {

    private GameBody game;

    public void initIndependent(GameBody game) throws Exception {
        this.game = game;
        game.rpcRecv().onFireAndForgetTemplate(RpcDebugPath.WORLD_FAF_GAME, DemoBananaVo.class, (head, banana) -> {
            log.info("A({})收到了一个FAF香蕉:{} head={}", game.config.srvId, banana, head);
        });
        game.rpcRecv().onFireAndForgetTemplate(RpcDebugPath.WORLD_FAF_GAME, DemoBananaVo.class, (head, banana) -> {
            log.info("B({})收到了一个FAF香蕉:{} head={}", game.config.srvId, banana, head);
        });
        game.rpcRecv().onFireAndForgetTemplate(RpcDebugPath.WORLD_FAF_GAME, DemoBananaVo.class, (head, banana) -> {
            log.info("C({})收到了一个FAF香蕉:{} head={}", game.config.srvId, banana, head);
        });

        game.rpcRecv().onRequestResponseTemplate(RpcDebugPath.WORLD_RR_GAME, DemoBananaVo.class, (head, banana) -> {
            log.info("X({})收到了一个RR香蕉:{} head={}", game.config.srvId, banana, head);
            DemoAppleVo apple = DemoAppleVo.random();
            apple.setType("巨苹果子");
            return apple;
        });
    }

    private int tickCount = 0;
    public void tick() throws Exception {
        // 暂时取消执行，测试基本完成
        if(++tickCount % 6 == 8) {
            DemoAppleVo apple1 = DemoAppleVo.random();
            apple1.setType("赠品苹果");
            game.rpcSend().fireAndForgetWorld(RpcDebugPath.GAME_FAF_WORLD, apple1);
            log.info("已发送FAF:{}", apple1);

            DemoAppleVo apple2 = DemoAppleVo.random();
            apple2.setType("礼品苹果");
            DemoBananaVo respBanana = game.rpcSend().requestResponseWorld(RpcDebugPath.GAME_RR_WORLD, apple2, DemoBananaVo.class);
            log.info("已发送RR:{}, 收到回复:{}", apple2, respBanana);
        }
    }
}
